package com.a2.appa3.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.ResRoomlist;
import com.a2.appa3.util.OnItemClickListener;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.shihao.library.XStatusBarHelper;

public class ChatRoomlistActivity extends BaseActivitySimple {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.roomuserlsit)
    RecyclerView roomuserlsit;
    Adapter adapter;
    private String grounpid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        if (android.os.Build.VERSION.SDK_INT >= 21) {
            //解决Android5.0以上，状态栏设置颜色后变灰的问题
            XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
        }
        headerTitle.setText("聊天成员");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        grounpid = getIntent().getStringExtra("grounpid");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        roomuserlsit.setLayoutManager(linearLayoutManager);
        adapter = new Adapter();
        getuserlist();
    }

    ArrayList<ResRoomlist.DataBean> list = new ArrayList();

    private void getuserlist() {
        HttpJsonUtils.getHttpApiInstance()
                .roomuserList(HttpAdress.getuseAdress() + "api/chatapi/ChatRoom/userList", grounpid, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResRoomlist>() {
                    @Override
                    protected void onCustomNext(ResRoomlist resRoomInfo) {
                        list.clear();
                        list.addAll(resRoomInfo.data);
                        roomuserlsit.setAdapter(adapter);
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_chat_roomlist;
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @NonNull
        @Override
        public Adapter.MViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new Adapter.MViewHolder(LayoutInflater.from(ChatRoomlistActivity.this)
                    .inflate(R.layout.chatlist_item, viewGroup, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Adapter.MViewHolder holder, int position) {
            holder.chat_name.setText(list.get(position).username + "");
            holder.timetext.setVisibility(View.GONE);
//            holder.acceptbt.setVisibility(View.GONE);
            Glide.with(ChatRoomlistActivity.this).load(HttpAdress.getuseAdress() +
                    list.get(position).img).error(R.mipmap.default_head).into(holder.chatimge);
            holder.chatcontent.setVisibility(View.GONE);
            holder.btnDelete.setVisibility(View.GONE);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView acceptbt, chatcontent, chat_name, timetext;
            ImageView chatimge;
            Button btnDelete;
            LinearLayout chat_linear;

            MViewHolder(View itemView) {
                super(itemView);
                chatimge = (ImageView) itemView.findViewById(R.id.user_img);
                acceptbt = (TextView) itemView.findViewById(R.id.acceptbt);
                chat_name = (TextView) itemView.findViewById(R.id.chat_name);
                timetext = (TextView) itemView.findViewById(R.id.timetext);
                btnDelete = (Button) itemView.findViewById(R.id.btnDelete);
                chatcontent = (TextView) itemView.findViewById(R.id.chatcontent);
                chat_linear = (LinearLayout) itemView.findViewById(R.id.chat_linear);
            }
        }
    }
}
